<?php

class Client extends FrontEnd_Controller {

    function Client() {
        parent::FrontEnd_Controller();
        $this->_data['erreur'] = "";
    }

    function index() {
        $this->profil();
    }
    function profil($action=""){
        is_user();
        $this->translate();
         $idclient = $this->session->userdata('clientid');
             $config = array(
            array('field' => 'prenom',
                'rules' => 'required|min_length[3]|max_length[12]|xss_clean'
            ),
            array('field' => 'nom',
                'rules' => 'required|min_length[3]|max_length[12]|xss_clean'
            ),
            array('field' => 'telephone',
                'rules' => 'required|xss_clean'
            ),
            array('field' => 'adresse',
                'rules' => 'required|xss_clean'
            )
        );
          $this->form_validation->set_rules($config);
         if($action=="enregistrer"){
           
                  if($this->form_validation->run()){
                  $data = array(
                            'nom'=>$this->input->post('nom'),
                            'prenom'=>$this->input->post('prenom'),
                            'tel'=>$this->input->post('telephone'),
                             'adresse'=>$this->input->post('adresse'),
                            );
               $query = $this->home_m->update_client($data,$idclient);

              }}
        $sidebar['1'] = "modules/gestion";
        
        $client = $this->home_m->get_client($idclient);
        $this->_data['client'] = $client;
        $this->_data['listsidebar'] = $sidebar;
        $this->display_view('profil');
    }

    function mot_de_pass($action=""){
         is_user();
         $this->translate();
          $idclient = $this->session->userdata('clientid');
             $config = array(
            array('field' => 'apass',
                'rules' => 'required|md5'
            ),
            array('field' => 'npass',
                'rules' => 'required|matches[confirm_npass]|md5'
            ),
            array('field' => 'confirm_npass',
                'rules' => 'required|md5'
            )
        );
              $this->form_validation->set_rules($config);
         if($action=="enregistrer"){
         if($this->form_validation->run()){
              $compte = array(
                    'id' => $idclient,
                    'pass' => $this->input->post('apass')
                );

           $query = $this->home_m->check_pass($compte);
           if ($query->num_rows() > 0) {
           $data = array( 'pass'=>$this->input->post('npass') );
            $query = $this->home_m->update_pass($data,$idclient);
           } else $this->_data['erreur'] = "Mot de passe incorrect!";
         }}
         $sidebar['1'] = "modules/gestion";
        $this->_data['listsidebar'] = $sidebar;
        $this->display_view('pass');
    }

    function commande() {
        is_user();
        $this->translate();
        $sidebar['1'] = "modules/gestion";
        $idclient = $this->session->userdata('clientid');


        //******PAGE CONFIG******************
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == ""
            )$per_page = 10;
        if ($offset == "")
            $offset = 0;
        $config['base_url'] = base_url() . 'index.php/client/commande/' . $per_page . '/';
        $config['per_page'] = '10';
        $config['uri_segment'] = 4;
        $config['full_tag_open'] = "<div class='contain_page'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="current_page"> [';
        $config['cur_tag_close'] = '] </span>';
        $config['next_link'] = '&gt;&gt;';
        $config['prev_link'] = '&lt;&lt;';
        $config['total_rows'] = $this->home_m->count_commande($idclient);
        $this->pagination->initialize($config);
        //**********************************************************
        $commande = $this->home_m->get_commande($idclient, $config['per_page'], $offset);

        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['commande'] = $commande;
        $this->_data['listsidebar'] = $sidebar;
        $this->display_view('commande');
    }

    function detail_commande($id) {
        is_user();
        
        $this->translate();
        $sidebar['1'] = "modules/gestion";
        $idclient = $this->session->userdata('clientid');


        //******PAGE CONFIG******************
        $per_page = $this->uri->segment(4);
        $offset = $this->uri->segment(5);
        if ($per_page == ""
            )$per_page = 10;
        if ($offset == "")
            $offset = 0;
        $config['base_url'] = base_url() . 'index.php/client/detail_commande/' . $id . '/' . $per_page . '/';
        $config['per_page'] = '10';
        $config['uri_segment'] = 5;
        $config['full_tag_open'] = "<div class='contain_page'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="current_page"> [';
        $config['cur_tag_close'] = '] </span>';
        $config['next_link'] = '&gt;&gt;';
        $config['prev_link'] = '&lt;&lt;';
        $config['total_rows'] = $this->home_m->count_produit_commande($idclient, $id);

        //**********************************************************
        $produit = $this->home_m->get_produit_commande($idclient, $id, $config['per_page'], $offset);
        $this->pagination->initialize($config);
//	$this->_data['pagination']=$this->pagination->create_links();	
        $this->_data['produit'] = $produit;
        $this->_data['listsidebar'] = $sidebar;
        $this->display_view('detail_commande');
    }

}